package com.jarics.test.client.entities;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.jarics.gwt.client.composites.CustomGridWidget;
import com.jarics.gwt.client.composites.Gridable;
import com.jarics.gwt.client.composites.JaricsMouseListener;

public class LabelAndComboBox extends Composite implements CustomGridWidget {

	private Label label;
	private ListBox listBox;
	
	int row;
	
	  /**
	 * Current table listener.
	 */
	private ArrayList<MouseListener> eventListeners;
	
	public void addChangeListener(ChangeListener listener) {
		listBox.addChangeListener(listener);
	}
	public void addClickListener(ClickListener listener) {
		listBox.addClickListener(listener);
	}
	//delegates for ListBox
	public void addItem(String item) {
		listBox.addItem(item);
	}
	public void setValue(int index, String value) {
		listBox.setValue(index, value);
	}
	public void clear() {
		listBox.clear();
	}
	public String getSelectedItemText() {
		return listBox.getItemText(listBox.getSelectedIndex());
	}
	public void setVisibleItemCount(int visibleItems) {
		listBox.setVisibleItemCount(visibleItems);
	}
	
	//delegates for Label
	public void setText(String text) {
		label.setText(text);
	}
	
	public void addAll(List<String> values){
		listBox.clear();
		for (String string : values) {
			listBox.addItem(string);
		}
	}
	
	
	public void setCustomGridWidget(String pTitle, boolean wrap, int row, Gridable gridable, FlexTable grid) {
		this.row = row;
		addMouseListener(new JaricsMouseListener(grid));
		sinkEvents(Event.ONMOUSEOVER);
		sinkEvents(Event.ONMOUSEOUT);
	}
	
	
	public LabelAndComboBox() {

		final VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setHeight("50px");
		verticalPanel.setSpacing(3);

		label = new Label("New Label");
		verticalPanel.add(label);

		listBox = new ListBox();
		verticalPanel.add(listBox);
		listBox.addItem("All...");
		listBox.addItem("Value1");
		listBox.addItem("Value2");
		listBox.addItem("Value3");
		listBox.addItem("Value4");

	}

	public int getRow() {
		return row;
	}

	public void setRow(int row) {
		this.row = row;
	}

	 
	public void onBrowserEvent(Event event) {
		System.out.println();
		switch (DOM.eventGetType(event)) {
		case Event.ONMOUSEOVER: {
			for (MouseListener listener : eventListeners) {
				listener.onMouseEnter(this);
			}
			break;
		}
		case Event.ONMOUSEOUT: {
			for (MouseListener listener : eventListeners) {
				listener.onMouseLeave(this);
			}
			break;
		}
		default: {
			// Do nothing
		}
		}
	}
	
	public void addMouseListener(MouseListener mouseListener){
		if (eventListeners == null){
			eventListeners = new ArrayList<MouseListener>();
		}
		eventListeners.add(mouseListener);
	}

}
